package ua.com.abakumov.android.prostopleer.engines.u

import java.net.{URI, URLEncoder}
import java.nio.charset.Charset
import org.apache.http.client.utils.URIUtils
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.HttpStatus
import java.io.ByteArrayOutputStream

/**
 *
 * User: aabakumov, Date: 26.12.12, Time: 16:55 
 */
class GetData extends GetDataAbstract {

  override def data(request: String,
           protocol: String,
           host: String,
           port: String,
           path: String,
           query: String): Option[String] = {

    try {
      val uri : URI  = (new UriHelper).createUri(protocol, host, path, query, request)

      val get: HttpGet = new HttpGet(uri)
      val response = new DefaultHttpClient().execute(get)

      val statusLine = response.getStatusLine
      statusLine.getStatusCode match {
        case HttpStatus.SC_OK => {
          val out: ByteArrayOutputStream = new ByteArrayOutputStream
          response.getEntity.writeTo(out)
          out.close()
          response.getEntity.getContent.close()

          Some(out.toString)
        }
        case _: Int => None
      }
    } catch {
      case e: Exception => {
        e.printStackTrace()
        None
      }
    }
  }
}
